package com.skstudio.report;

import java.util.ArrayList;

public class ReportCpu extends ReportBase {
	public static class DataCpu {
		public long mCoreId = 0;
		public long mDeltaUser = 0, mDeltaKernel = 0, mDeltaTotal = 0;
		public float mUserPctage = 0.f;
		public float mKernelPctage = 0.f;
		
		public void Read(byte[] buffer, int offset) {
			mCoreId = getCxxUnsignedLong(buffer, offset + 0);
			mDeltaUser = getCxxUnsignedLong(buffer, offset + 4);
			mDeltaKernel = getCxxUnsignedLong(buffer, offset + 8);
			mDeltaTotal = getCxxUnsignedLong(buffer, offset + 12);
			
			mUserPctage = (float) (mDeltaUser * 100.0) / (float)(mDeltaTotal);
			mKernelPctage = (float) (mDeltaKernel * 100.0) / (float)(mDeltaTotal);
		}
		
		public float getTotalUsage() {
			return mUserPctage + mKernelPctage;
		}
	}
	
	public ArrayList < ArrayList < DataCpu > > mDataList = new ArrayList < ArrayList < DataCpu > >();

	@Override
	public void parse(byte[] buffer, int offset)
	{
		DataCpu dc = new DataCpu();
		dc.Read(buffer, offset);
		if (dc.mCoreId >= mDataList.size()) {
			ArrayList < DataCpu > qdc= new ArrayList < DataCpu >();
			mDataList.add(qdc);
		}
		ArrayList < DataCpu > qdc = mDataList.get((int)dc.mCoreId);
		if (qdc.size() == getMaxQueueSize()) {
			qdc.remove(0);
		}
		qdc.add(dc);	
	}
	
	@Override
	public void reset() {
		mDataList.clear();
	}
}
